package com.djx.基础;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

/**
 * 节点删除前提是 节点没有子节点
 *
 */
public class ZKDelete {

    ZooKeeper zooKeeper = new MyZookepper01().zooKeeper;

    /**
     * 同步删除节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void syncDeleteNode() throws KeeperException, InterruptedException {
        zooKeeper.delete("/lrh/aaa", -1);
        zooKeeper.close();
        System.out.println("节点删除成功");
    }

    /**
     * 异步删除节点
     * @throws InterruptedException
     */
    @Test
    public void asyncDeleteNode() throws InterruptedException {

        zooKeeper.delete("/lrh/bbb", -1, new AsyncCallback.VoidCallback() {
            public void processResult(int i, String s, Object o) {
                System.out.println(i);
                System.out.println(s);
                System.out.println(o);
            }
        }, "删除成功");
        Thread.sleep(10000);
        zooKeeper.close();
        System.out.println("节点删除成功");
    }
}
